﻿Imports System
Imports System.CodeDom
Imports System.Text
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.Activities.Rules
    Friend Class ExpressionStatement
        Inherits RuleCodeDomStatement
        ' Methods
        Private Sub New(ByVal exprStatement As CodeExpressionStatement)
            Me.exprStatement = exprStatement
        End Sub

        Friend Overrides Sub AnalyzeUsage(ByVal analysis As RuleAnalysis)
            RuleExpressionWalker.AnalyzeUsage(analysis, Me.exprStatement.Expression, False, False, Nothing)
        End Sub

        Friend Overrides Function Clone() As CodeStatement
            Return New CodeExpressionStatement { _
                .Expression = RuleExpressionWalker.Clone(Me.exprStatement.Expression) _
            }
        End Function

        Friend Shared Function Create(ByVal statement As CodeStatement) As RuleCodeDomStatement
            Return New ExpressionStatement(DirectCast(statement, CodeExpressionStatement))
        End Function

        Friend Overrides Sub Decompile(ByVal decompilation As StringBuilder)
            If (Me.exprStatement.Expression Is Nothing) Then
                Dim exception As New RuleEvaluationException(Messages.InvokeStatementNull)
                exception.Data.Item("ErrorObject") = Me.exprStatement
                Throw exception
            End If
            RuleExpressionWalker.Decompile(decompilation, Me.exprStatement.Expression, Nothing)
        End Sub

        Friend Overrides Sub Execute(ByVal execution As RuleExecution)
            RuleExpressionWalker.Evaluate(execution, Me.exprStatement.Expression)
        End Sub

        Friend Overrides Function Match(ByVal comperand As CodeStatement) As Boolean
            Dim statement As CodeExpressionStatement = [TryCast](comperand,CodeExpressionStatement)
            Return ((Not statement Is Nothing) AndAlso RuleExpressionWalker.Match(Me.exprStatement.Expression, statement.Expression))
        End Function

        Friend Overrides Function Validate(ByVal validation As RuleValidation) As Boolean
            Dim flag As Boolean = False
            If (Me.exprStatement.Expression Is Nothing) Then
                Dim error As New ValidationError(Messages.NullInvokeStatementExpression, &H53D)
                [error].UserData.Item("ErrorObject") = Me.exprStatement
                validation.Errors.Add([error])
                Return flag
            End If
            If TypeOf Me.exprStatement.Expression Is CodeMethodInvokeExpression Then
                Return (Not RuleExpressionWalker.Validate(validation, Me.exprStatement.Expression, False) Is Nothing)
            End If
            Dim item As New ValidationError(Messages.InvokeNotHandled, &H548)
            item.UserData.Item("ErrorObject") = Me.exprStatement
            validation.Errors.Add(item)
            Return flag
        End Function


        ' Fields
        Private exprStatement As CodeExpressionStatement
    End Class
End Namespace

